﻿using Fiddler;
using System;
using System.Windows.Forms;

namespace iFiddlerX.Properties
{
    /// <summary>
    /// iFiddlerX插件，保持类公开以使Fiddler可见
    /// </summary>
    public class IFiddlerMainClass : IAutoTamper
    {
        #region 初始化数据
        IFiddlerMainForm IFD = new IFiddlerMainForm();
        ToolStripButton TsrStartIFD = new ToolStripButton();
        ToolStripButton TsrRemoveAllSession = new ToolStripButton();
        #endregion
        public IFiddlerMainClass()
        {
            /*在此安全的初始化数据*/

            /* 载入iFiddler保存的规则*/
            IFD.LoadRules();

            /* 清空所有链接按钮*/
            TsrRemoveAllSession.DisplayStyle = ToolStripItemDisplayStyle.ImageAndText;//显示方式为图像和文字
            TsrRemoveAllSession.Text = "清空所有链接";//按钮命名
            TsrRemoveAllSession.Image = Resources.RemoveAllSession;//按钮图像
            FiddlerToolbar.AddToolStripItem(TsrRemoveAllSession);//增加按钮到FiddlerToorBar
            TsrRemoveAllSession.Click += new EventHandler(TsrRemoveAllSession_Click);//绑定单击事件

            /* 启动iFiddlerX按钮*/
            TsrStartIFD.DisplayStyle = ToolStripItemDisplayStyle.ImageAndText;//显示方式为图像和文字
            TsrStartIFD.Text = "启动" + IRabbit.Title;//按钮命名
            TsrStartIFD.Image = Resources.StartIFD;//按钮图像
            FiddlerToolbar.AddToolStripItem(TsrStartIFD);//增加按钮到FiddlerToorBar
            TsrStartIFD.Click += new EventHandler(TsrStartIFD_Click);//绑定单击事件

            IFD.LogMain(IRabbit.Title + "载入完毕!");
        }
        /// <summary>
        /// 载入插件时
        /// </summary>
        public void OnLoad()
        {

        }

        /// <summary>
        /// 【启动iFiddlerX】按钮的单击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TsrStartIFD_Click(object sender, EventArgs e)
        {
            IFD.Show();//显示iFiddler窗口
        }

        /// <summary>
        /// 【清空所有链接】按钮的单击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TsrRemoveAllSession_Click(object sender, EventArgs e)
        {
            FiddlerApplication.UI.actRemoveAllSessions();
        }

        public void OnBeforeUnload() { }
       
        /// <summary>
        /// Request前
        /// </summary>
        /// <param name="oSession"></param>
        public void AutoTamperRequestBefore(Session oSession)
        {
            IFD.AutoRules(oSession);//自动修改规则(ResponseBefore方法中也调用一次来修改返回值)
        }
       
        /// <summary>
        /// Request后
        /// </summary>
        /// <param name="oSession"></param>
        public void AutoTamperRequestAfter(Session oSession)
        {
            IFD.AutoGetCookies(oSession);//自动取出Cookies
        }
       
        public void AutoTamperResponseBefore(Session oSession)
        {
            IFD.AutoRules(oSession);//自动修改规则(修改返回值)
        }
       
        /// <summary>
        /// response后
        /// </summary>
        /// <param name="oSession"></param>
        public void AutoTamperResponseAfter(Session oSession)
        {
            IFD.AutoGetResponseText(oSession);//自动取出返回文本
        }
        
        public void OnBeforeReturningError(Session oSession)
        {

        }
    }
}
